package org.mtmi.ui.scenes;

import org.mtmi.tuio.listeners.TUIOTapEvent;
import org.mtmi.ui.controls.SideButton;

/**
 * a Side Button that sick on scenes: when user change to another scene
 * this button remain displayed and available.
 * <br>
 * In addition this button can be moved only when scene is editing
 */
public abstract class AbstractGlobalSceneSideButton extends SideButton
  implements ISceneManagerListener
{

	/** create scene side button, register it in scene manager listener */
	public AbstractGlobalSceneSideButton( Scene scene, int flags ) {
		super(scene, flags );
		SceneManager.getDefault().addSceneManagerListener( this );
		
		if( scene.isEditing() ) {
			setForeground( COLOR.yellow );
			setBackground( CLR.darker( getForeground(), 0.5f ) );
			//can move button only when editing:
			setCanMoveButton( true );
		} else {
			setForeground( COLOR.light_blue );
			setBackground( CLR.darker( getForeground(), 0.5f ) );
			setCanMoveButton( false ); //require edit mode
		}
	}
	
	@Override
	public void dispose() {
		SceneManager.getDefault().removeSceneManagerListener( this );
		super.dispose();
	}
	
	@Override
	public void sceneManagerModeChanged( boolean edit ) {
		if( edit ) {
			setForeground( COLOR.yellow );
		} else {
			setForeground( COLOR.light_blue );
		}
		//can move button only when editing:
		setCanMoveButton( edit );
		setBackground( CLR.darker( getForeground(), 0.5f ) );
		redraw();
	}
	
	@Override
	public void sceneManagerSceneChanged( Scene new_scene, Scene old_scene ) {
		//I want this button available for all scenes
		setScene( new_scene );
	}

	@Override
	public void tapUp( TUIOTapEvent event ) {
		boolean was_down = isTapped && !isMoving();
		super.tapUp( event );
		if( was_down ) {
			doAction();
		}
	}
	
	/** subclasses override this method to do the job, without adding button listener */ 
	protected abstract void doAction();

	@Override
	public void sceneManagerSceneAdded(Scene sceneAdded) {}

	@Override
	public void sceneManagerSceneRemoved(Scene sceneRemoved) {}

	
}
